import React, { Component } from 'react';
import PublistForm from './components/PublistForm';
import { hashHistory } from 'react-router';
import { Loading } from '../commons'
import { DownloadService, ChannelService, TaskService } from '../../services'
import { getUser } from '../../auth'

export default class Publish extends Component {
  static displayName = 'Publish';

  constructor(props) {
    super(props);
    this.state = {
      loading : true,
      channel : [],
    };
  }

  componentDidMount = async() =>{
    let user = getUser()
    let channel = await ChannelService.getChannelWithEnable()
    channel = channel.map(c=>{return { label : c.name, value : c.id}})
    channel = channel.filter(c=> user.channel.indexOf(c.value) != -1)
    this.setState({ channel, loading : false })
  }

  onSubmit = async(data) =>{
    let user = getUser()
    await TaskService.publish({
      channel : data.channel,
      file : data.file.file,
      name : data.file.name,
      company : user.account.company,
      username : user.user
    })
    hashHistory.push('/publish/list')
    return true
  }

  render() {
    return (
      <div className="publish-page">
        <Loading loading={this.state.loading}/>
        <PublistForm headers={TaskService.getHeader()} downloadPath={DownloadService.downloadPath} channel={this.state.channel} onSubmit={this.onSubmit}/>
      </div>
    );
  }
}

const styles = {
  
}